home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 11 - 1995 / 11.02 Feb 95 / Yenta / C Libraries / ConvertTools.c next >
Encoding:
C/C++ Source or Header  |  1996-04-04  |  2.8 KB  |  122 lines  |  [TEXT/KAHL]

  1. #include <ConvertTools.h>
  2. #include <MathTools.h>
  3.  
  4. /*=======================================================================*/
  5. /*= This file contains a set of utilities for converting text & strings =*/
  6. /*=======================================================================*/
  7.  
  8.     void    CToPString (char *CStr, Str255 *PStr, short maxLen)
  9.     /* convert maxLen characters of the the c string passed in CStr into a */
  10.     /* pascal-syle string.  PStr must already by allocated */
  11.     {
  12.         short    len;
  13.         
  14.         if (PStr && *PStr && CStr)
  15.           {
  16.               len = Min (strlen(CStr), maxLen);
  17.               BlockMove (CStr, &((*PStr)[1]), len);
  18.               **PStr = len;
  19.           }
  20.     }
  21.     
  22. /*------------------------------------------------------------------------------*/
  23.  
  24.     void    PToCString (StringPtr PString, char *CStr, short maxLen)
  25.     /* convert maxLen characters of the the pascal string passed in PStr into a */
  26.     /* c-syle string.  CStr must already by allocated */
  27.     {
  28.         short    len;
  29.         
  30.         if (PString && CStr)
  31.           {
  32.               len = Min (maxLen, *PString);
  33.               BlockMove (PString+1, CStr, len);
  34.               CStr[len] = '\0';
  35.           }
  36.     }
  37.  
  38. /*------------------------------------------------------------------------------*/
  39.  
  40.     StringPtr Data2PString (void *InData, short DataLen)
  41.     /* copy the passed data into a new Pascal string */
  42.     {
  43.         Ptr NewPtr = NULL;
  44.         
  45.         if (InData)
  46.           {
  47.             NewPtr = NewPtrClear(DataLen+1);
  48.             if (NewPtr)
  49.               {
  50.                 BlockMove(InData, NewPtr+1, DataLen);
  51.                 NewPtr[0] = DataLen;
  52.                 return (StringPtr)NewPtr;
  53.               }
  54.             else
  55.               return NULL;
  56.           }
  57.         else
  58.           return NULL;
  59.     }
  60.     
  61. /*------------------------------------------------------------------------------*/
  62.  
  63.     Ptr Data2Ptr (void *InData, short DataLen)
  64.     /* copy the passed data into a new pointer */
  65.     {
  66.         Ptr    NewPtr = NULL;
  67.         
  68.         if (InData)
  69.           {
  70.             NewPtr = NewPtrClear(DataLen);
  71.             if (NewPtr)
  72.               {
  73.                 BlockMove(InData, NewPtr, DataLen);
  74.                 return NewPtr;
  75.               }
  76.             else
  77.               return NULL;
  78.           }
  79.         else
  80.           return NULL;
  81.     }
  82.  
  83. /*------------------------------------------------------------------------------*/
  84.  
  85.     Handle    PString2Text (StringPtr TheString)
  86.     /* make a copy of the string and store it in handle with no length byte */
  87.     {
  88.         Handle    H = NULL;
  89.         
  90.         H = NewHandleClear(*TheString);
  91.         if (H)
  92.           BlockMove(TheString+1, *H, *TheString);
  93.         return H;
  94.     }
  95.  
  96. /*------------------------------------------------------------------------------*/
  97.  
  98.     StringPtr Text2PString (Handle TheString)
  99.     /* copy the text in the handle into a Pascal string */
  100.     {
  101.         long numBytes;
  102.         StringPtr    STemp = NULL;
  103.         
  104.         if (!TheString) return NULL;
  105.         
  106.         numBytes = GetHandleSize(TheString);
  107.         
  108.         if (TheString)
  109.           {
  110.             STemp = (StringPtr)NewPtrClear(numBytes+1);
  111.             if (STemp)
  112.               {
  113.                   BlockMove(*TheString, &(STemp[1]), numBytes);
  114.                   STemp[0] = (short)numBytes;
  115.               }
  116.           }
  117.         return STemp;
  118.     }
  119.  
  120. /*------------------------------------------------------------------------------*/
  121.  
  122.